package cn.stronglink.assetmanage.business.setting.syncset;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.SyncContent;

/**
 * @author zww
 * 2018 2018/6/18 8:48
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class SystemSyncContentAdapter extends RecyclerView.Adapter<SystemSyncContentHolder> {
    public List<SyncContent> list = new ArrayList<>();
    private SyncListener listener;
    private Context mContext;

    SystemSyncContentAdapter(Context context) {
        this.mContext = context;
        if (DaoManager.getDaoSession(context).getSyncContentDao().loadAll().size() == 0) {
            initDatas();
            for (SyncContent entity :
                    list) {
                DaoManager.getDaoSession(context).getSyncContentDao().insert(entity);
            }
        } else {
            list = DaoManager.getDaoSession(context).getSyncContentDao().loadAll();
        }
    }

    private void initDatas() {
/*        SyncContent s1 = new SyncContent();
        s1.setImgCheck(R.drawable.checked);
        s1.setImgUnCheck(R.drawable.uncheck);
        s1.setIsSelected(false);
        s1.setSyscType(1);
        s1.setImgShow(R.drawable.task);
        s1.setSysText("我的任务");
        s1.setImgUnShow(R.drawable.task_press);
        list.add(s1);*/

        SyncContent s1 = new SyncContent();
        s1.setImgCheck(R.drawable.checked);
        s1.setImgUnCheck(R.drawable.uncheck);
        s1.setIsSelected(false);
        s1.setSyscType(1);
        s1.setImgShow(R.drawable.enter_goable_un);
        s1.setSysText("验收入库");
        s1.setImgUnShow(R.drawable.maintenance);
        list.add(s1);

        SyncContent s2 = new SyncContent();
        s2.setImgCheck(R.drawable.checked);
        s2.setImgUnCheck(R.drawable.uncheck);
        s2.setIsSelected(false);
        s2.setSyscType(2);
        s2.setImgShow(R.drawable.enter_goable_un);
        s2.setSysText("盘点管理");
        s2.setImgUnShow(R.drawable.maintenance);
        list.add(s2);

        SyncContent s3 = new SyncContent();
        s3.setImgCheck(R.drawable.checked);
        s3.setImgUnCheck(R.drawable.uncheck);
        s3.setIsSelected(false);
        s3.setSyscType(3);
        s3.setImgShow(R.drawable.enter_goable_un);
        s3.setSysText("巡检维修");
        s3.setImgUnShow(R.drawable.maintenance);
        list.add(s3);

        SyncContent s4 = new SyncContent();
        s4.setImgCheck(R.drawable.checked);
        s4.setImgUnCheck(R.drawable.uncheck);
        s4.setIsSelected(false);
        s4.setSyscType(4);
        s4.setImgShow(R.drawable.enter_goable_un);
        s4.setSysText("基础数据");
        s4.setImgUnShow(R.drawable.maintenance);
        list.add(s4);

/*        SyncContent s5 = new SyncContent();
        s5.setImgCheck(R.drawable.checked);
        s5.setImgUnCheck(R.drawable.uncheck);
        s5.setIsSelected(false);
        s5.setSyscType(5);
        s5.setImgShow(R.drawable.enter_goable_un);
        s5.setSysText("资产数据");
        s5.setImgUnShow(R.drawable.maintenance);
        list.add(s5);*/
    }

    @Override
    public SystemSyncContentHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View inflate = View.inflate(mContext, R.layout.item_system_content, null);
        return new SystemSyncContentHolder(inflate, listener);
    }

    @Override
    public void onBindViewHolder(SystemSyncContentHolder holder, int position) {
        if (null != list) {
            if (!list.get(position).getIsSelected()) {
                holder.imgSync.setImageResource(list.get(position).getImgShow());
                holder.txtSyncContent.setText(list.get(position).getSysText());
                holder.imgCheck.setImageResource(list.get(position).getImgUnCheck());
                holder.itemView.setBackgroundResource(R.drawable.shape_corner_default);
                holder.txtSyncContent.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
            } else {
                holder.imgSync.setImageResource(list.get(position).getImgUnShow());
                holder.txtSyncContent.setText(list.get(position).getSysText());
                holder.imgCheck.setImageResource(list.get(position).getImgCheck());
                holder.itemView.setBackgroundResource(R.drawable.shape_corner);
                holder.txtSyncContent.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
            }
        }
    }

    @Override
    public int getItemCount() {
        if (null != list) {
            return list.size();
        }
        return 0;
    }

    public void setOnItemClickListener(SyncListener listener) {
        this.listener = listener;
    }
}
